/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DbPort;

import DataTypes.AutoDiaryDataTypes.AmbiAct;
import DataTypes.AutoDiaryDataTypes.AmbiLiteral;
import DataTypes.AutoDiaryDataTypes.WeightMatrix;
import TransmissionPort.FillSecondaryData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author thang
 */
public class Test {

    private static    long time = 1332330000; // importance
    
    private static    long gpsData_id = 0;
    private static    long place_id = 0;
    private static    int type_id = 0;
    private static    double mLat = -37.8090476;    // sydney -33.8670522;
    private static    double mLong = 144.9998405;   // sydney 151.1957362;
    private static    int  mRadius = 50;
    private static    int   weight = 0;
    private static    int   level = 0;
    private static    int   window_index = 0;
    private static    String device_id = "samsungGalaxyII";
    private static    String code = "00";
    private static    int windowSlides = 2;

    public static void main(String[] args){
        ConnectionManager.connectDb();
        
        insertActLog();
        //DataWindowManager.slideWindow(windowSlides);
        //testInsert2();
        //insertLayer();
        
        ConnectionManager.disconnectDb();
    }
    
    public static void insertActLog(){
        WeightMatrix input = new WeightMatrix();
        
        /*
        input.time = 133007;
        input.device_id = autodiaryserver.Declare.DEVICE_ID;
        input.matrix = new ArrayList<AmbiLiteral>();
        
        AmbiLiteral a = new AmbiLiteral();
        a.code = "walking";
        a.memberValue = 0.7;
        input.matrix.add(a);
        
        a = new AmbiLiteral();
        a.code = "driving";
        a.memberValue = 0.7;
        input.matrix.add(a);
        * 
        */

        input.time = time;
        input.device_id = autodiaryserver.Declare.DEVICE_ID;
        input.matrix = new ArrayList<AmbiLiteral>();
        
        AmbiLiteral a = new AmbiLiteral();
        a.code = "running";
        a.memberValue = 0.6;
        input.matrix.add(a);
        
        a = new AmbiLiteral();
        a.code = "stayingStill";
        a.memberValue = 0.7;
        input.matrix.add(a);
        
        
        DataReceiver.receiveData(input);
    }
    
    public static void insertLayer(){
        try {
            
            // insert into table primActivities
            DataTypes.AutoDiaryDataTypes.PrimaryActivity pa = new DataTypes.AutoDiaryDataTypes.PrimaryActivity();
            pa.setActivityCode(code);
            pa.setSensorTimeStamp(time);
            DataReceiver.receiveData(pa);

            // insert into table gpsData
            DataTypes.AutoDiaryDataTypes.Gps g = new DataTypes.AutoDiaryDataTypes.Gps();
            g.setGpsAccuracy(20);
            g.setGpsLat(mLat);
            g.setGpsLong(mLong);
            g.setGpsSpeed(3);
            g.setGpsTime(time);
            gpsData_id = DataReceiver.receiveData(g);
            
            FillSecondaryData.fill(gpsData_id, mLat, mLong, mRadius);
          
        } catch(Exception e){ System.out.println("Error, OK ?!!!"); }
    }

    public static void testInsert(){
        try {
            // insert into table primActivities
            DataTypes.AutoDiaryDataTypes.PrimaryActivity pa = new DataTypes.AutoDiaryDataTypes.PrimaryActivity();
            pa.setActivityCode(code);
            pa.setSensorTimeStamp(time);
            DataReceiver.receiveData(pa);

            // insert into table gpsData
            DataTypes.AutoDiaryDataTypes.Gps g = new DataTypes.AutoDiaryDataTypes.Gps();
            g.setGpsAccuracy(20);
            g.setGpsLat(mLat);
            g.setGpsLong(mLong);
            g.setGpsSpeed(3);
            g.setGpsTime(time);
            gpsData_id = DataReceiver.receiveData(g);

            // insert into table places
            DataTypes.AutoDiaryDataTypes.Place p = new DataTypes.AutoDiaryDataTypes.Place();
            p.setDistance(10);
            p.setGpsDataId(gpsData_id);
            p.setLatitude(mLat);
            p.setLongitude(mLong);
            p.setName("PlaceName");
            place_id = DataReceiver.receiveData(p);

            // insert into table types
            DataTypes.AutoDiaryDataTypes.Type t = new DataTypes.AutoDiaryDataTypes.Type();
            t.setType("Test_Type");
            type_id = DataReceiver.receiveData(t);
            System.out.println( "" + DataReceiver.getDTypesID("Test_Type") );        

            // insert into table placesTypes
            DataReceiver.receivePlacesTypes(place_id, type_id);

            // insert into table address
            DataTypes.GoogleApiDataTypes.Address a = new DataTypes.GoogleApiDataTypes.Address();

            a.formatted_address = "Test address";
            DataReceiver.receiveAddress(a, gpsData_id, -1, -1);

            // try to update table address
            DataReceiver.updateAddress(gpsData_id, place_id, "place_id");
            //DataReceiver.updateAddress(-1, place_id, "gpsData_id");
            
        } catch(Exception e){ System.out.println("Error, OK ?!!!"); }
        
    }    

    public static void testInsert2(){
        List<AmbiAct> al = new ArrayList<AmbiAct>();
        
        try {
            AmbiAct aa = new AmbiAct();
            
            aa.setDevice_id(device_id);
            aa.setActivityCode(code);
            aa.setSensorTimeStamp(time);
            aa.setGpsId(gpsData_id);
            aa.setPlaceId(place_id);
            aa.setWeight(weight);
            aa.setLevel(level);
            al.add(aa);
            
            aa = new AmbiAct();
            
            aa.setDevice_id(device_id);
            aa.setActivityCode("s001");
            aa.setSensorTimeStamp(time);
            aa.setGpsId(gpsData_id);
            aa.setPlaceId(place_id);
            aa.setWeight(weight);
            aa.setLevel(level);
            al.add(aa);
            
            //aa.setWindowIndex(window_index);
            //DataReceiver.receiveData(aa);
            DataWindowManager.insertData(al);
            
        } catch(Exception e){ System.out.println("Error, OK ?!!!"); }
        
    }

    
    public static void clearTest(){
        
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        try{
            // get keys
            
            // get gpsData_id
            ps = Declare.mCon.prepareStatement("select id from d_gpsData where time = ?");
            ps.setLong(1, time);
            rs = ps.executeQuery();
            
            if (rs.first()){
                gpsData_id = rs.getLong(1);
            }
            
            // get place_id
            ps.close();
            ps = Declare.mCon.prepareStatement("select id from d_places where gpsData_id = ?");
            ps.setLong(1, gpsData_id);
            rs = ps.executeQuery();
            
            if (rs.first()){
                place_id = rs.getLong(1);
            }
            
            // clear d_actWindow
            ps = Declare.mCon.prepareStatement("delete from d_actWindow where device_id = ?");
            ps.setString(1, device_id);
            ps.executeUpdate();
            
            
            // clear address
            ps.close();
            ps = Declare.mCon.prepareStatement("delete from d_address where gpsData_id = ?");
            ps.setLong(1, gpsData_id);
            ps.executeUpdate();
            
            // clear table placesTyeps
            ps.close();
            ps = Declare.mCon.prepareStatement("delete from d_placesTypes where place_id = ?");
            ps.setLong(1, place_id);
            ps.executeUpdate();
            
            // clear table types
            ps.close();
            ps = Declare.mCon.prepareStatement("delete from d_pTypes where type = ?");
            ps.setString(1, "Test_Type");
            ps.executeUpdate();
            
            // clear table places
            ps.close();
            ps = Declare.mCon.prepareStatement("delete from d_places where gpsData_id = ?");
            ps.setLong(1, gpsData_id);
            ps.executeUpdate();
            
            // clear table gpsData
            ps.close();
            ps = Declare.mCon.prepareStatement("delete from d_gpsData where id = ?");
            ps.setLong(1, gpsData_id);
            ps.executeUpdate();
            
            // clear table primitiveActivities
            ps.close();
            ps = Declare.mCon.prepareStatement("delete from d_primActivities where time = ?");
            ps.setLong(1, time);
            ps.executeUpdate();
            

            
        } catch (SQLException ex) {
            Logger.getLogger(DataReceiver.class.getName()).log(Level.SEVERE, null, ex);
        } finally{ 
            if (ps != null) { try { ps.close(); } catch (SQLException ex) {}}  
        }

    }
    
    public static void clearTest2(){
        
        PreparedStatement ps = null;
        
        try{
            // get keys
            
            // clear d_actWindow
            ps = Declare.mCon.prepareStatement("delete from d_actWindow where device_id = ?");
            ps.setString(1, device_id);
            ps.executeUpdate();
            
            
        } catch (SQLException ex) {
            Logger.getLogger(DataReceiver.class.getName()).log(Level.SEVERE, null, ex);
        } finally{ 
            if (ps != null) { try { ps.close(); } catch (SQLException ex) {}}  
        }
    }
    
}
